/*
 * Project: daycare-ent
 * Package: com.made2ordersoftware.daycare.domain
 * File: ParentEntity
 * Created on: Sep 15, 2013
 */
/**
 * Copyright (c) 2010 Made 2 Order Software, LLC
 *
 * All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 * transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 * You may not distribute the URL or redistribute the files to third parties. You may transfer the files onto
 * a single storage device so long as you can prove ownership of the software. You may not reverse engineer,
 * decompile, or disassemble the software or media files. You may not modify or translate the software or
 * media, or distribute copies of the software or media without the written consent of Made 2 Order Software,
 * LLC.
 */
package com.made2ordersoftware.daycare.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Entity class representing the a parent which has children registered with the day care.
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
@Entity
@DiscriminatorValue(value = "PARENT")
@NamedQuery(name = "Parent.findParentIDs", query = "SELECT p.id FROM ParentEntity p")
@XmlRootElement
public class ParentEntity extends PersonEntity {

    private static final Logger LOG = LoggerFactory.getLogger(ParentEntity.class);
    private static final long serialVersionUID = 1L;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
    private Collection<PhoneEntity> phoneNumbers;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
    private Collection<AddressEntity> addresses;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
    private Collection<EmailEntity> emails;
    @JoinTable(name = "parent_child", joinColumns = {@JoinColumn(name = "parent_id")}, inverseJoinColumns = {@JoinColumn(name = "child_id")})
    @ManyToMany(cascade = CascadeType.ALL)
    private Collection<ChildEntity> children;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "child")
    private Collection<TimeWatchedEntity> timeWatched;

    /**
     * Creates an instance of <code>ParentEntity</code>.
     */
    public ParentEntity() {
    }

    /**
     * Creates an instance of <code>ParentEntity</code>.
     *
     * @param firstName First name of parent
     * @param lastName  Last name of parent
     */
    public ParentEntity(String firstName, String lastName) {
        super(firstName, lastName);
    }

    /**
     *
     * @return list of phone numbers
     */
    @XmlTransient
    public Collection<PhoneEntity> getPhoneNumbers() {
        return Collections.unmodifiableCollection(phoneNumbers);
    }

    /**
     * Sets the phoneNumbers
     *
     * @param phoneNumbers List of parent's phone numbers.
     */
    public void setPhoneNumbers(Collection<PhoneEntity> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }

    /**
     * Adds a phone number to this parent's phoneNumbers list.
     *
     * @param phone The phone number to add to the list
     */
    public void addPhoneEntity(PhoneEntity phone) {
        if (this.phoneNumbers == null) {
            this.phoneNumbers = new ArrayList<PhoneEntity>();
        }
        this.phoneNumbers.add(phone);
    }

    /**
     * Removes a phone number from this parent's phoneNumbers list.
     *
     * @param phone The phone number to add to the list
     */
    public void removePhone(PhoneEntity phone) {
        if (this.phoneNumbers != null) {
            this.phoneNumbers.remove(phone);
        }
    }

    /**
     *
     * @return list of addresses
     */
    @XmlTransient
    public Collection<AddressEntity> getAddresses() {
        return Collections.unmodifiableCollection(addresses);
    }

    /**
     * Sets the addresses
     *
     * @param addresses List of partent's addresses.
     */
    public void setAddresses(Collection<AddressEntity> addresses) {
        this.addresses = addresses;
    }

    /**
     * Adds an address to this parent's addresses list.
     *
     * @param address The address to add to the list
     */
    public void addAddressEntity(AddressEntity address) {
        if (this.addresses == null) {
            this.addresses = new ArrayList<AddressEntity>();
        }
        this.addresses.add(address);
    }

    /**
     * Removes an address from this parent's addresses list.
     *
     * @param address The address to add to the list
     */
    public void removeAddress(AddressEntity address) {
        if (this.addresses != null) {
            this.addresses.remove(address);
        }
    }

    /**
     *
     * @return list of emails
     */
    @XmlTransient
    public Collection<EmailEntity> getEmails() {
        return Collections.unmodifiableCollection(emails);
    }

    /**
     * Sets the emails
     *
     * @param emails List of partent's emails.
     */
    public void setEmails(Collection<EmailEntity> emails) {
        this.emails = emails;
    }

    /**
     * Adds a email to this parent's emails list.
     *
     * @param email The email to add to the list
     */
    public void addEmail(EmailEntity email) {
        if (this.emails == null) {
            this.emails = new ArrayList<EmailEntity>();
        }
        this.emails.add(email);
    }

    /**
     * Removes an email from this parent's emails list.
     *
     * @param email The email to add to the list
     */
    public void removeEmail(EmailEntity email) {
        if (this.emails != null) {
            this.emails.remove(email);
        }
    }

    /**
     * Returns the list of children associated with this parent.
     *
     * @return The Collection of ChildEntity object for this parent
     */
    @XmlTransient
    public Collection<ChildEntity> getChildren() {
        return Collections.unmodifiableCollection(children);
    }

    /**
     * Sets the list of children associated with this parent.
     *
     * @param children The Collection of ChildEntity objects for this parent
     */
    public void setChildren(Collection<ChildEntity> children) {
        this.children = children;
    }

    /**
     * Adds a child to this parent's children list.
     *
     * @param child The ChildEntity to add to the list
     */
    public void addChild(ChildEntity child) {
        if (this.children == null) {
            this.children = new ArrayList<ChildEntity>();
        }
        this.children.add(child);
    }

    /**
     * Removes a child from this parent's children list.
     *
     * @param child The ChildEntity to add to the list
     */
    public void removeChild(ChildEntity child) {
        if (this.children != null) {
            this.children.remove(child);
        }
    }

    /**
     *
     * @return list of times watched
     */
    @XmlTransient
    public Collection<TimeWatchedEntity> getTimeWatched() {
        return Collections.unmodifiableCollection(timeWatched);
    }

    /**
     * Sets the timeWatched
     *
     * @param timeWatched List of time watch records for partent.
     */
    public void setTimeWatched(Collection<TimeWatchedEntity> timeWatched) {
        this.timeWatched = timeWatched;
    }

    /**
     * Adds a time watched to this parent's timeWatched list.
     *
     * @param timeWatched The timeWatched to add to the list
     */
    public void addTimeWatched(TimeWatchedEntity timeWatched) {
        if (this.timeWatched == null) {
            this.timeWatched = new ArrayList<TimeWatchedEntity>();
        }
        this.timeWatched.add(timeWatched);
    }

    /**
     * Removes a Time Watched from this parent's timeWatched list.
     *
     * @param timeWatched The timeWatchedEntity to add to the list
     */
    public void removeTimeWatched(TimeWatchedEntity timeWatched) {
        if (this.timeWatched != null) {
            this.timeWatched.remove(timeWatched);
        }
    }

    /* (non-Javadoc)
     * @see com.made2ordersoftware.daycare.domain.PersonEntity#hashCode()
     */
    @Override
    public int hashCode() {
        int hash = 9;
        hash = 71 * hash + Objects.hashCode(this.getId());
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof ParentEntity)) {
            return false;
        }
        ParentEntity other = (ParentEntity) object;
        return Objects.equals(this.getId(), other.getId());
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(super.toString());
        if (addresses != null) {
            for (AddressEntity address : addresses) {
                sb.append(address.toString());
            }
        }
        if (phoneNumbers != null) {
            for (PhoneEntity number : phoneNumbers) {
                sb.append(number.toString());
            }
        }
        if (emails != null) {
            for (EmailEntity email : emails) {
                sb.append(email.toString());
            }
        }
        sb.append("}");
        return sb.toString();
    }

}
